<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class close_payment extends Page
{
       public $edDescription = null;
       public $compid = null;
       public $JTDivAccount = null;
       public $JTGroupBox4 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $btnShowAccount = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $JTSiteTheme1 = null;
       public $hiddenReminder = null;
       public $hiddenVeh = null;
       public $Image3 = null;
       public $cmbAccount = null;
       public $lblAccount = null;
       public $edAmount = null;
       public $lblAmount = null;
       public $memoComments = null;
       public $lblComments = null;
       public $lblDescription = null;
       public $dpDate = null;
       public $lblDate = null;
       public $cmbType = null;
       public $lblType = null;
       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,comp);
       <?php

       }

       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var compider = document.getElementById('compid').value;
       AjaxAccount2(addaccount,compider);
       document.close_payment.edAccount.value= "";
       <?php

       }


       function btnShowAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivAccount').Show();
       document.close_payment.edAccount.value= "";
       <?php

       }




       function btnCancelJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.close();
       <?php

       }


       function btnSubmitClick($sender, $params)
       {

       mysql_query("INSERT INTO veh_expense_tbl(vehicle_id,
       type,
       amount,
       date,
       account,
       description,
       comments)

       VALUES
       ('".$this->hiddenVeh->getValue()."',
       '".$this->cmbType->getItemIndex()."',
       '".$this->edAmount->Text."',
       '".$this->dpDate->Text."',
       '".$this->cmbAccount->getItemIndex()."',
       '".$this->edDescription->Text."',
       '".$this->memoComments->Text."')") or die("INSERT EXPENSE - ".mysql_error());


       mysql_query("UPDATE veh_reminder_tbl SET
       status2 = 'Complete'
       WHERE reminder_id = '".$this->hiddenReminder->getValue()."'")or die("UPDATE REMINDER".mysql_error());


       // create new veh_reminder
       $query_rec_reminder = "select * from veh_reminder_tbl where reminder_id='".$this->hiddenReminder->getValue()."'";
       //die($query_rec_reminder);
       $result_rec_query = mysql_query($query_rec_reminder)or die ("veh_remind - ".mysql_error());
       $row_rec_reminder = mysql_fetch_array($result_rec_query);

       $temp_rec_id = $row_rec_reminder['recurring_id'];

       $query_rec = "select * from template_recurring_tbl where id='".$temp_rec_id."'";
       //die($query_rec_reminder);
       $result_rec = mysql_query($query_rec)or die ("veh_remind - ".mysql_error());
       $row_rec = mysql_fetch_array($result_rec);

       $query_veh = "select * from vehicle_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'";
       //die($query_rec_reminder);
       $result_veh = mysql_query($query_veh)or die ("vehicle - ".mysql_error());
       $row_veh = mysql_fetch_array($result_veh);


       $flag = $row_rec_reminder['flag_temp'];

       if($flag=="1")
       {

       $sched_by = $row_rec['schedule_by'];

       $temp_od = $row_rec['due_usage'];
       $veh_od = $row_rec_reminder['due_hours'];
       $new_od = $veh_od+$temp_od;


       if($sched_by=="Mileage" || $sched_by=="Hours")
       {
       $odometer = $new_od;
       }

       if($sched_by=="Time")
       {
       $odometer = '0';
       }


       $basis = date('Y-m-d');
       $days_mos = $row_rec['due_by'];
       $val = $row_rec['due_time'];

       if($days_mos=='Days')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." day");
       $due_date = date("Y-m-d", $date_computed);
       }

       if($days_mos=='Months')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." month");
       $due_date = date("Y-m-d", $date_computed);
       }

       if($sched_by=='Mileage' || $sched_by=='Hours')
       {
         $save_rem = "rem_hours,";
         $save_rem2 = "'".$row_rec['rem_usage']."',";
       }
       if($sched_by=='Time')
       {
         $save_rem = "rem_by,rem_time,";
         $save_rem2 = "'".$row_rec['rem_by']."','".$row_rec['rem_time']."',";
       }

       mysql_query("insert into veh_reminder_tbl (company_id,
       vehicle_id,
       schedule_by,
       due_hours,
       due_date,
       ".$save_rem."
       completion_window,
       type,
       description,
       price,
       status2,
       flag_temp,
       recurring_id)

       values ('".$_SESSION['company_id']."',
       '".$this->hiddenVeh->getValue()."',
       '".$row_rec_reminder['schedule_by']."',
       '".$odometer."',
       '".$due_date."',
       ".$save_rem2."
       '".$row_rec_reminder['completion_window']."',
       '".$row_rec_reminder['type']."',
       '".$row_rec_reminder['description']."',
       '".$row_rec_reminder['price']."',
       'Pending',
       '1',
       '".$row_rec['id']."')")or die("insert reminder - ".mysql_error());
       //die($new_od);
       //die($query_ins_remin);
       //$result = mysql_query($query_ins_remin)or die("INSERT REMINDER - " . mysql_error());
       }




         if($row_rec_reminder['recur_per_veh']!='0')
         {
         $sched_by = $row_rec_reminder['schedule_by'];

         $every_od = $row_rec_reminder['every_hours'];
         $due_od = $row_rec_reminder['due_hours'];
         $new_od = $due_od+$every_od;


         if($sched_by=="Mileage" || $sched_by=="Hours")
         {
         $odometer_veh = $new_od;
         }

         if($sched_by=="Time" || $sched_by=="Date")
         {
         $odometer_veh = '0';
         }


         $basis_date = date('Y-m-d');
         $days_mos = $row_rec_reminder['every_by'];
         $val = $row_rec_reminder['every_date'];

         if($days_mos=='Days')
         {
         $date = date($basis_date);
         $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." day");
         $due_date_veh = date("Y-m-d", $date_computed);
         }

         if($days_mos=='Months')
         {
         $date = date($basis_date);
         $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." month");
         $due_date_veh = date("Y-m-d", $date_computed);
         }


         if($sched_by=='Mileage' || $sched_by=='Hours')
         {
           $save_rem = "rem_hours,";
           $save_rem2 = "'".$row_rec_reminder['rem_hours']."',";
         }
         if($sched_by=='Time' || $sched_by=='Date')
         {
           $save_rem = "rem_by,rem_time,";
           $save_rem2 = "'".$row_rec_reminder['rem_by']."','".$row_rec_reminder['rem_time']."',";
         }

         mysql_query("insert into veh_reminder_tbl (company_id,
         vehicle_id,
         schedule_by,
         recur_per_veh,
         due_hours,
         due_date,
         every_hours,
         every_date,
         every_by,
         ".$save_rem."
         completion_window,
         type,
         description,
         vendor,
         comments,
         status2
         )

         values ('".$_SESSION['company_id']."',
         '".$this->hiddenVeh->getValue()."',
         '".$sched_by."',
         '1',
         '".$odometer_veh."',
         '".$due_date_veh."',
         '".$row_rec_reminder['every_hours']."',
         '".$row_rec_reminder['every_date']."',
         '".$row_rec_reminder['every_by']."',
         ".$save_rem2."
         '".$row_rec_reminder['completion_window']."',
         '".$row_rec_reminder['type']."',
         '".$row_rec_reminder['description']."',
         '".$row_rec_reminder['vendor']."',
         '".$row_rec_reminder['comments']."',
         'Pending')")or die("insert reminder - ".mysql_error());
         }



       if($flag!="1")
       {
       }

       // end veh_reminder


       echo '<script language=javascript>
       alert("Reminder completed!");
       //window.opener.location.reload();
       //window.top.location.reload();
       window.close()
       window.top.opener.top.location.reload();
       </script>';


       }



       function close_paymentCreate($sender, $params)
       {
       $this->compid->Value = $_SESSION['company_id'];
       $veh_id = $_GET['veh'];
       $reminder_id = $_GET['reminder'];
       $this->hiddenVeh->setValue($veh_id);
       $this->hiddenReminder->setValue($reminder_id);

       $result_reminder = mysql_query("SELECT * FROM veh_reminder_tbl where reminder_id='".$this->hiddenReminder->getValue()."'")or die ("SELECT REMINDER".mysql_error());
       $row_reminder = mysql_fetch_array($result_reminder);

       $this->cmbType->Clear();
       $this->cmbType->setItemIndex($row_reminder['type']);
       $result_type = mysql_query("select value from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND category='types'");

        while($row_type = mysql_fetch_array($result_type))
          {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
          }


        $this->cmbAccount->Clear();
        $result_account = mysql_query("select value from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

        while($row_account = mysql_fetch_array($result_account))
        {
            $this->cmbAccount->AddItem($row_account['value'], null, $row_account['value']);
        }

        //list account
        $result_a = mysql_query("select value from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

        while($row_a = mysql_fetch_array($result_a))
        {
            $this->listAccount->AddItem($row_a['value'], null, $row_a['value']);
        }

       $this->edAmount->Text = $row_reminder['price'];
       $this->dpDate->Text = date('Y-m-d');
       $this->memoComments->Text='';




       }


}

global $application;

global $close_payment;

//Creates the form
$close_payment=new close_payment($application);

//Read from resource file
$close_payment->loadResource(__FILE__);

//Shows the form
$close_payment->show();

?>